package entities;

import handlers.B2dVars;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.me.mygdxgame.Game;

public class HUD {

	private Player player;
	private TextureRegion[] blocks;
	private TextureRegion container;
	private TextureRegion crystal;
	private TextureRegion[] font;// lay chu va so trong buc hinh ra

	public HUD(Player player) {
		this.player = player;

		Texture tex = Game.res.getTexture("hud");
		container = new TextureRegion(tex, 0, 0, 32, 32);

		blocks = new TextureRegion[3];
		for (int i = 0; i < blocks.length; i++) {
			blocks[i] = new TextureRegion(tex, 32 + i * 16, 0, 16, 16);
		}

		crystal = new TextureRegion(tex, 80, 0, 16, 16);
		font = new TextureRegion[11];

		font = new TextureRegion[11];
		for (int i = 0; i < 6; i++) {
			font[i] = new TextureRegion(tex, 32 + i * 9, 16, 9, 9);
		}
		for (int i = 0; i < 5; i++) {
			font[i + 6] = new TextureRegion(tex, 32 + i * 9, 25, 9, 9);
		}
	}

	public void render(SpriteBatch sb) {

		sb.begin();
		// ve container
		sb.draw(container, 32, 200);
		// ve block
		short bits = player.getBody().getFixtureList().first().getFilterData().maskBits;

		if ((bits & B2dVars.BIT_RED) != 0) {
			sb.draw(blocks[0], 40, 208);
		}
		if ((bits & B2dVars.BIT_GREEN) != 0) {
			sb.draw(blocks[1], 40, 208);
		}
		if ((bits & B2dVars.BIT_BlUE) != 0) {
			sb.draw(blocks[2], 40, 208);
		}

		// ve crystal
		sb.draw(crystal, 100, 208);
		drawString(sb, player.getNumCtystal() + "/" + player.getTotalCrystal(),
				132, 211);

		sb.end();

	}

	private void drawString(SpriteBatch sb, String s, float x, float y) {
		for (int i = 0; i < s.trim().length(); i++) {
			char c = s.charAt(i);
			if (c == '/')
				c = 10;
			else if (c >= '0' && c <= '9')
				c -= '0';
			else
				continue;
			sb.draw(font[c], x + i * 9, y);
		}
	}
}
